package com.leetcode.algorithm.y22.m06;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 515. 在每个树行中找最大值
 * 
 * https://leetcode.cn/problems/find-largest-value-in-each-tree-row/
 * 
 * @author jie.deng
 *
 */
class Question0555Solution01 {
	
	public List<Integer> largestValues(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		if (root == null) {
			return list;
		}

		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(root);

		while (!queue.isEmpty()) {
			int size = queue.size();
			int max = Integer.MIN_VALUE;
			for (int i = 0; i < size; i++) {
				TreeNode cur = queue.poll();
				if (cur.left != null) {
					queue.add(cur.left);
				}
				if (cur.right != null) {
					queue.add(cur.right);
				}
				if (cur.val > max) {
					max = cur.val;
				}
			}
			list.add(max);
		}

		return list;
	}
    
}